package com.mall.cart.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mall.cart.service.CartService;
import com.mall.manager.dao.TbItemMapper;
import com.mall.manager.pojo.TbItem;
import com.mall.pojo.TaotaoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */

@Service(timeout = 10000000)
@Component
@Transactional
public class CartServiceImp implements CartService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    TbItemMapper itemMapper;

    @Value("${cart_prefix}")
    private String cart_prefix;


    @Override
    public TaotaoResult addItemToCart(long userId, long itemId,int num) {

        //判断redis中该key和hashkey是否存在
        String key = cart_prefix+userId;
        String hashKey=itemId+"";

        //判断是否有该key
        Boolean hasKey = stringRedisTemplate.opsForHash().hasKey(key, hashKey);
        if (hasKey){
            //更新该商品数量
            String itemJsonStr = (String) stringRedisTemplate.opsForHash().get(key, hashKey);
            TbItem item = JSONObject.parseObject(itemJsonStr, TbItem.class);
            item.setNum(item.getNum() + 1);
            stringRedisTemplate.opsForHash().put(key,hashKey,JSONObject.toJSONString(item));
            return TaotaoResult.ok();
        }

        //不存在该key
        TbItem item = itemMapper.selectByPrimaryKey(itemId);
        //设置为当前选择的item数量
        item.setNum(num);
        stringRedisTemplate.opsForHash().put(key,hashKey,JSONObject.toJSONString(item));
        return TaotaoResult.ok();
    }

    @Override
    public List<TbItem> showCart(long userId) {
        String key = cart_prefix + userId;
        List<Object> jsonItems = stringRedisTemplate.opsForHash().values(key);
        List<TbItem> items = new ArrayList<>();
        for (Object jsonItem : jsonItems) {
            TbItem item = JSONObject.parseObject((String) jsonItem, TbItem.class);
            items.add(item);
        }
        return items;
    }

    @Override
    public void mergeItems(List<TbItem> cartItems, long userId) {
        //将cookie中的商品往redis中添加即可完成合并
        if (cartItems != null){
            for (TbItem cartItem : cartItems) {
                addItemToCart(userId,cartItem.getId(),cartItem.getNum());
            }
        }
    }

    @Override
    public void updateCart(long userId, long itemId, int num) {
        String key = cart_prefix + userId;
        String hashKey = itemId + "";
        String itemJson = (String) stringRedisTemplate.opsForHash().get(key, hashKey);
        //服务端购物车中含有该商品信息
        if (itemJson != null){
            TbItem item = JSONObject.parseObject(itemJson, TbItem.class);
            item.setNum(num);
            stringRedisTemplate.opsForHash().put(key,hashKey,JSONObject.toJSONString(item));
        }
        //服务器购物车未包含该信息
        TbItem item = itemMapper.selectByPrimaryKey(itemId);
        item.setNum(num);
        stringRedisTemplate.opsForHash().put(key,hashKey,JSONObject.toJSONString(item));
    }

    @Override
    public void deleteCartItem(long userId, long itemId) {
        String key = cart_prefix + userId;
        String hashKey = itemId + "";
        stringRedisTemplate.opsForHash().delete(key,hashKey);
    }
}
